package com.lenovo.tool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class TranslationFilter
{
	private Set<String> list;
	
	public static void main(String[] args)
	{
		TranslationFilter filter = new TranslationFilter();
		filter.filte(args);
    }

	private void filte(String[] args)
	{
		if (args.length != 1) {
            System.out.println("please enter the config file name");
            return;
        }

        File configFile = new File(args[0]);
        if (!configFile.exists() || !configFile.isFile()) {
            System.out.println("config file error");
            return;
        }

        try {
            PropConfigHandler handler = new PropConfigHandler();

            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser parser = factory.newSAXParser();
            parser.parse(configFile, handler);

            Properties props = handler.getProps();

            File baseDir = new File(props.getProperty("basedir"));
			String baseDirString = props.getProperty("basedir");
			System.out.println("Find dir = " + baseDirString);
			
            if (!baseDir.exists()) {
                System.out.println("config file error");
                return;
            }

            if(baseDir.isFile())
            {
            	System.out.println("is not is directionary");
            	return;
            }
            list = new HashSet<String>();
            filterFiles(baseDir);
            if(list.size() == 0)
            {
            	System.out.println("no xml files int current directionary");
            }
            else
            {
            	String[] fileNames = new String[list.size()];
            	list.toArray(fileNames);
            	for (int i = 0; i < fileNames.length; i++)
				{
            		System.out.println(fileNames[i]);
				}
            	writeLog(list);
            }
        } 
        catch (ParserConfigurationException e) {
            e.printStackTrace();
        } 
        catch (SAXException e) {
            e.printStackTrace();
        } 
        catch (IOException e) {
            e.printStackTrace();
        }
	}

	private void filterFiles(File baseDir)
	{
		File[] files = baseDir.listFiles();
		String fileName;
		for (File file : files)
		{
			if(file.isFile())
			{
				if( isStringResDir(file.getParent()) && file.getAbsolutePath().endsWith(".xml"))
				{
					fileName = file.getAbsolutePath().replace('\\', '/');
					fileName = fileName.substring( fileName.lastIndexOf('/') + 1);
					list.add(fileName);
				}
			}
			else
			{
				filterFiles(file);
			}
		}
	}

	/**
	 * 判断是否为字符资源目录
	 * @param parent
	 * @return
	 */
	private boolean isStringResDir(String parent)
	{
		parent = parent.replace('\\', '/');
		parent = parent.substring(parent.lastIndexOf('/') + 1);
		return parent.startsWith("values");
	}

	private void writeLog(Set<String> logs)
	{
		FileWriter writer = null;
		try
		{
			writer = new FileWriter("./xml_filter_result.log", false);
			String fileName;
			for (Iterator<String> it = logs.iterator(); it.hasNext();)
			{
				fileName = it.next();
				writer.write(fileName + "\n");
			}
			writer.flush();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(writer != null)
			{
				try
				{
					writer.close();
				} catch (IOException e)
				{
					e.printStackTrace();
				}
			}
		}
	}
}
